/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.resrev.service.impl;

import com.rafkind.resrev.data.Reservation;
import com.rafkind.resrev.data.User;
import com.rafkind.resrev.service.MailService;
import java.text.MessageFormat;
import java.util.Date;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

/**
 *
 * @author Fictional Hero
 */
public class MailServiceImpl implements MailService {
  
  private MailSender mailSender;
  private String from;
  private String homeUrl;
  private String title;
  
  public void sendTestMail(String from, String to) {
    SimpleMailMessage message = new SimpleMailMessage();
    
    message.setFrom(from);
    message.setTo(to);
    message.setReplyTo(from);
    message.setSentDate(new Date());
    message.setSubject("Test email");
    message.setText("Test email sent successfully!");
        
    mailSender.send(message);
  }

  public MailSender getMailSender() {
    return mailSender;
  }

  public void setMailSender(MailSender mailSender) {
    this.mailSender = mailSender;
  }

  public void notifyApproved(Reservation reservation) {
    final SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(reservation.getReserver().getEmail());        
    message.setSentDate(new Date());
    message.setSubject(MessageFormat.format("{0} has approved your reservation for {1}.",
        reservation.getAuthorizor().getName(),
        reservation.getResource().getName()));
    message.setText(MessageFormat.format(
        "Hello, {0}!\n\n"
        + "{1} has approved your reservation for {2} from {3} to {4}.\n\n"        
        + "Thanks,\n{5}\n{6}",
        reservation.getReserver().getName(),
        reservation.getAuthorizor().getName(),
        reservation.getResource().getName(),
        reservation.getStart(),
        reservation.getEnd(),
        title,
        homeUrl
        ));

    new Thread() {
          @Override
          public void run() {        
            mailSender.send(message);
          }
        }.start();
  }

  public void notifyCreated(Reservation reservation) {
    // notify authorizors
    
    
    User[] users = { reservation.getReserver().getAuthorizor(),
      reservation.getResource().getAuthorizor() };
    
    // avoid sending the same mail twice
    if (users[0] == users[1])
      users[1] = null;
    
    for (int i = 0; i < users.length; i++) {
      User user = users[i];
      if (user != null && !reservation.getReserver().equals(user)) {
        final SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(user.getEmail());        
        message.setSentDate(new Date());
        message.setSubject(MessageFormat.format("{0} has reserved {1} and needs your approval.",
            reservation.getReserver().getName(),
            reservation.getResource().getName()));
        message.setText(MessageFormat.format(
            "Hello, {0}!\n\n"
            + "{1} has reserved {2} from {3} to {4}.\n\n"
            + "Please approve or deny this reservation by logging in to:"
            + "\n{5}\n"
            + "Thanks,\n{6}",
            user.getName(),
            reservation.getReserver().getName(),
            reservation.getResource().getName(),
            reservation.getStart(),
            reservation.getEnd(),
            homeUrl,
            title
            ));
        
        new Thread() {
          @Override
          public void run() {        
            mailSender.send(message);
          }
        }.start();
      }
    }    
  }

  public void notifyDenied(Reservation reservation) {
    final SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(reservation.getReserver().getEmail());        
    message.setSentDate(new Date());
    message.setSubject(MessageFormat.format("Your reservation for {1} has been denied by {0}.",
        reservation.getAuthorizor().getName(),
        reservation.getResource().getName()));
    message.setText(MessageFormat.format(
        "Hello, {0}!\n\n"
        + "{1} has denied your reservation for {2} from {3} to {4}.\n\n"        
        + "Thanks,\n{5}\n{6}",
        reservation.getReserver().getName(),
        reservation.getAuthorizor().getName(),
        reservation.getResource().getName(),
        reservation.getStart(),
        reservation.getEnd(),
        title,
        homeUrl
        ));

    new Thread() {
          @Override
          public void run() {        
            mailSender.send(message);
          }
        }.start();
  }

  public String getFrom() {
    return from;
  }

  public void setFrom(String from) {
    this.from = from;
  }

  public String getHomeUrl() {
    return homeUrl;
  }

  public void setHomeUrl(String homeUrl) {
    this.homeUrl = homeUrl;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }
}